<?php


namespace app\privatearea\controller\v1;


use app\common\cachemodel\privatearea\PrivateAreaGoods;
use app\common\cachemodel\privatearea\PrivateAreaQuestionInfo;
use app\common\lib\AliOss;
use app\privatearea\controller\Auth;

class Good extends Auth
{
    protected $avoidLoginAction = ['goodintro'];
    protected $filterSn = ['64473d89c2401', '644658ac32d80', '6448b691a067d'];

    /**
     * 获取商品介绍
     */
    public function goodIntro()
    {
        $sn = input('sn/s', '');
        $isOrderView = input('is_order_view/d', 0);
        $time = $this->request->time();

        $goodModel = new PrivateAreaGoods();

        $goodInfo = $goodModel->getPrivateAreaGoodInfo($sn, ['uid', 'order_num', 'is_pay', 'lid', 'img', 'content', 'channel_id', 'create_time', 'type', 'price', 'name']);

        if (!in_array($sn, $this->filterSn, true))
        {
            if (!$goodInfo || ($goodInfo['is_pay'] === 0 && $goodInfo['create_time'] + 86400 < $time))
            {   // 判断链接是否失效 如果未支付 且超过失效 24小时
                return show(config('code.success'), '请求成功', ['status' => 0]);
            }
        }
        if ($this->user)
        {   // 已登录
            if ($goodInfo['uid'])
            {
                if ($this->user['id'] !== (int)$goodInfo['uid'])
                {   // 非指定用户
                    return show(config('code.success'), '请求成功', ['status' => 0]);
                }
            } else
            {
                // 绑定当前用户ID
                $goodModel->updatePrivateAreaLinkData($sn, $goodInfo['lid'], ['uid' => $this->user['id']]);
            }
            if ($goodInfo['order_num'] && $isOrderView === 0)
            {
                if ($goodInfo['is_pay'] === 1)
                {   // 已支付 查看订单详情
                    return show(config('code.success'), '请求成功', ['status' => 2, 'order_id' => $goodInfo['order_num'], 'order_status' => 1]);
                } else
                {
                    // 判断订单 状态 是否是待支付状态
                    return show(config('code.success'), '请求成功', [
                        'status' => 2,
                        'order_id' => $goodInfo['order_num'],
                        'order_status' => 0,
                    ]);
                }
            }
        }

        return show(config('code.success'), '请求成功', [
            'status' => 1,
            'name' => $goodInfo['name'],
            'img' => $goodInfo['img'],
            'content' => $goodInfo['content'],
            'type' => $goodInfo['type'],
            'price' => $goodInfo['price'],
            'channel_id' => $goodInfo['channel_id'],
        ]);
    }

    /**
     * 商品信息接口
     */
    public function goodInfo()
    {
        $sn = input('sn/s', '');

        $goodModel = new PrivateAreaGoods();
        $goodInfo = $goodModel->getPrivateAreaGoodInfo($sn, ['uid', 'lid', 'type', 'price', 'name', 'extend', 'info_type', 'card_num', 'order_num', 'create_time', 'is_pay']);

        if (!in_array($sn, $this->filterSn, true))
        {
            if (!$goodInfo || ($goodInfo['is_pay'] === 0 && $goodInfo['create_time'] + 86400 < $this->request->time()))
            {   // 判断链接是否失效 如果未支付 且超过失效 24小时
                return show(config('code.success'), '请求成功', ['status' => 0]);
            }
        }


        if ($goodInfo['uid'])
        {
            if ($this->user['id'] !== $goodInfo['uid'])
            {
                return show(config('code.success'), '请求成功', ['status' => 0]);
            }
        } else
        {
            // 绑定当前用户ID
            $goodModel->updatePrivateAreaLinkData($sn, $goodInfo['lid'], ['uid' => $this->user['id']]);
        }

        if ($goodInfo['is_pay'] === 1)
        {   // 已支付 查看订单详情
            return show(config('code.success'), '请求成功', ['status' => 2, 'order_id' => $goodInfo['order_num'], 'order_status' => 1]);
        }

        // 获取之前输入的内容
        $questionInfo = (new PrivateAreaQuestionInfo())->getUserQuestionData($this->user['id'], $sn);
        $qid = 0;
        if ($questionInfo)
        {
            if ($questionInfo['status'] === 1)
            {
                return show(config('code.success'), '请求成功', ['status' => 2, 'order_id' => $goodInfo['order_num'], 'order_status' => 0]);
            }
            $qid = $questionInfo['status'] > 0 ? 0 : $questionInfo['id'];
            if ($goodInfo['type'] === 5)
            {   // 骰子不记录
                $questionInfo = [];
            }
        }

        $supplementInfo = [];
        if (isset($goodInfo['extend']['supplement_info']))
        {
            foreach ($goodInfo['extend']['supplement_info'] as $item)
            {
                if (isset($questionInfo['supplement_info'][$item['id']]))
                {
                    if ((int)$item['type'] === 1)
                    {   // 选择题
                        if ((int)$item['content']['is_multi_choice'] === 1)
                        {   // 多选
                            $item['content']['default_value'] = array_column($questionInfo['supplement_info'][$item['id']]['option'], 'id');
                        } else
                        {   // 单选
                            $item['content']['default_value'] = $questionInfo['supplement_info'][$item['id']]['option']['id'];
                        }
                    } else
                    {   // 填空提
                        $item['content']['default_value'] = $questionInfo['supplement_info'][$item['id']]['content'];
                    }
                } else
                {
                    $item['content']['default_value'] = '';
                }

                $supplementInfo[] = $item;
            }
        }

        $recodeList = [];
        $nameAndPicList = [];
        $cardList = [];
        if ($questionInfo && isset($questionInfo['question_data']) && $questionInfo['question_data'])
        {
            $aliOss = AliOss::getInstance();
            if ($questionInfo['question_data']['recode1'])
            {
                $recodeList[] = [
                    'rid' => $questionInfo['question_data']['recode1']['id'],
                    'avatar' => $questionInfo['question_data']['recode1']['avatar'],
                    'name' => $questionInfo['question_data']['recode1']['name'],
                ];
            }
            if ($questionInfo['question_data']['recode2'])
            {
                $recodeList[] = [
                    'rid' => $questionInfo['question_data']['recode2']['id'],
                    'avatar' => $questionInfo['question_data']['recode2']['avatar'],
                    'name' => $questionInfo['question_data']['recode2']['name'],
                ];
            }

            if ($questionInfo['question_data']['name1'] && $questionInfo['question_data']['photo1'])
            {
                $nameAndPicList[] = [
                    'avatar' => $questionInfo['question_data']['photo1'],
                    'url' => str_replace('http://zhunle.oss-cn-beijing.aliyuncs.com', 'https://oss.goddessxzns.com', $aliOss->getAuth($questionInfo['question_data']['photo1'])),
                    'name' => $questionInfo['question_data']['name1']
                ];
            }

            if ($questionInfo['question_data']['name2'] && $questionInfo['question_data']['photo2'])
            {
                $nameAndPicList[] = [
                    'avatar' => $questionInfo['question_data']['photo2'],
                    'url' => str_replace('http://zhunle.oss-cn-beijing.aliyuncs.com', 'https://oss.goddessxzns.com', $aliOss->getAuth($questionInfo['question_data']['photo2'])),
                    'name' => $questionInfo['question_data']['name2']
                ];
            }

            if ($questionInfo['question_data']['cards'])
            {
                $cardList = json_decode($questionInfo['question_data']['cards'], true);
                $cardList = $cardList ?: [];
            }
        }


        return show(config('code.success'), '请求成功', [
            'status' => 1,
            'type' => $goodInfo['type'],
            'info_type' => $goodInfo['info_type'],
            'price' => $goodInfo['price'],
            'card_num' => $goodInfo['card_num'],
            'supplement_info' => $supplementInfo,
            'default_data' => [
                'recode_list' => $recodeList,
                'name_and_pic_list' => $nameAndPicList,
                'card_list' => $cardList,
                'qid' => $qid,
            ],
        ]);
    }
}
